package com.example.administrator.fragmenttest319hz.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.fragmenttest319hz.R;
import com.example.administrator.fragmenttest319hz.bean.TcaseBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13.
 */

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;//上下文
    private LayoutInflater LayoutInflater;
    private List<TcaseBean> list;
    //1构造方法
    public MyAdapter(Context context){
        this.context=context;
        LayoutInflater= android.view.LayoutInflater.from(context);
    }
    //2获取数据
    public void setList(List<TcaseBean> list){

        this.list=list;
    }

    //3ViewHolder子类
    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView tvtitle,tvdescript,tvtime;
        private Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tvtitle = (TextView) itemView.findViewById(R.id.textView);
            tvdescript = (TextView) itemView.findViewById(R.id.textView2);
            tvtime = (TextView) itemView.findViewById(R.id.textView3);
            button = (Button) itemView.findViewById(R.id.button);

        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=LayoutInflater.inflate(R.layout.item,parent,false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        TcaseBean TcaseBean=list.get(position);
    if (TcaseBean==null){
        return;
    }

        ViewHolder viewHolder = (ViewHolder)holder;
       // viewHolder.imageView.setImageResource(TcaseBean.getImgid());
        viewHolder.tvtitle.setText(TcaseBean.getName());
        viewHolder.tvdescript.setText(TcaseBean.getDescription());
        viewHolder.tvtime.setText(TcaseBean.getUpdate_time());

        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/"+TcaseBean.getThumb())
                .placeholder(R.mipmap.ic_launcher).into(viewHolder.imageView);

        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();//更新Recycyler
                //在实际案例中 ，要删除应该会涉及访问网络或数据库
                //要有删除前的提醒
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id=list.get(position).getId();
                Toast.makeText(context, ""+id, Toast.LENGTH_SHORT).show();
                //实际中，点击后打开窗口做其他操作
            }
        });

    }

    @Override
    public int getItemCount() {
        if(list==null) return 0;

        else
            return list.size();
}

}
